﻿using System;
using System.Collections;
using System.Data;
using System.Windows;
using System.Windows.Threading;
using SLS.Base;
using SLS.Document;
using SLS.ExClassLib.Event;
using SLS.ExClassLib.ExThreading;
using SLS.ExClassLib.Message;
namespace SLS.Core.HLASimu
{
    public class UIFederate : Federate
    {
        public UIFederate()
           :base()
        {
        }
        public UIFederate(string m_ClientType, string m_FEDName, object m_RTIServiceClient)
            :base(m_ClientType,m_FEDName,m_RTIServiceClient)
        {
       
        }
        public override void SendMessage(MessageEx msg)
        {
            try
            {
                base.SendMessage(null);
            }
            catch (Exception ex)
            {
                throw;
            }
        }
        public override ArrayList GetMessage(bool FIFO = true)
        {
            ArrayList msgList = base.GetMessage(FIFO);
            foreach (MessageEx msg in msgList)
            {
                if (msg != null)
                {
                     //sun20121002
                    SLBC doc = null;
                    doc = (SLBC)DocumentManagement.Instance.FindDocumentByAddress(FEDName,"");
                    if (doc != null)
                    {
                        doc.ProcessUIMessage(msg);
                    } 
                }
            }
            return msgList;
        }
        private void POPUIMessage()
        {
            try
            {
           
                //Task t = Task.Factory.StartNew(() => { msg=GetMessage(); });
                this.Dispatcher.BeginInvoke(() => { GetMessage(true); }, DispatcherPriority.ContextIdle);
               //MessagePool按时间戳排序
               //获取最小时间戳消息
               //获取服务器上此联邦成员最小时间戳MinTimeStamp
               //如果当前时间戳=最小时间戳，则处理这个消息，否则等待下一个消息到达或者处理完消息之后再触发
               
            }
            catch (Exception ex)
            {
                throw;
            }
        }
        public override void ReceiveMessage(MessageEx msg)
        {
            try
            {
                base.ReceiveMessage(msg);
                MessageEx msgA = EventDispatcher.Instance.MessagePool.Find(msg.MessageID_Req);
                if (msgA != null)
                {
                    msg=EndUpMessage(msgA, msg);
                }
                POPUIMessage();
            }
            catch (Exception ex)
            {
                throw;
            }
        }
        public override void work_ChangedWorkItemState(object sender, ChangedWorkItemStateEventArgs e)
        {
            base.work_ChangedWorkItemState(sender, e);
            lock (this)
            {
                ExWorkItem exWinWI = (ExWorkItem)e.WorkItem;
                WorkItemState state = exWinWI.State;
                if (state == WorkItemState.Completed)
                {
                    MessageEx msgNew = exWinWI.ThisMessage;
                    string JobType = msgNew.JobType; 
                    if (JobType == "UI2UI")
                    {
                        Application.Current.Dispatcher.BeginInvoke(m_ReceiveMessage, DispatcherPriority.ContextIdle, msgNew);
                    } 
                }
            }
        }
        public override void BuildWorkQueue(MessageEx msg)
        {
            base.BuildWorkQueue(msg); 
        }
        public override void MakeUpMessage(string CallerType, ref MessageEx msg, object Data = null,string Action="")
        {
            base.MakeUpMessage(CallerType, ref msg, Data,Action);
            msg.JobType = "UI2UI";
            DataSet setEvent = new DataSet();
            switch (Action)
            {
                case "OnGetNextMessage":
                    msg.JobType = "FED2RTI";
                    POPUIMessage();
                    break;
                case "OnJoinFederation":
                    msg.JobType = "FED2RTI";
                    break;
                case "OnSetUpSet5Property":
                    setEvent = Data as DataSet;
                    msg.setEvent = setEvent;
                    break;
                case "OnKeyRelaitonComplete":
                case "OnSaveActiveRecord":
                case "OnLoadSet2Create":
                case "OnLoadSet4Reference":
                case "OnLoadSet5KeyDef":
                case "OnLoadForeignKeyTable":  
                case "OnStartDocument":
                case "OnSetDefaultActiveRecord": 
                case "OnCreateGoodsMasterKeyRelation":
                case "OnLoadCreatedRecord": 
                case "OnMenuSelected":
                case"OnCreateNewRecord":
                default:
                  msg.Tag = Data;
                    break;
            } 
        }
        public MessageEx EndUpMessage(MessageEx msgA, MessageEx msgB)
        {
           return base.EndUpMessage(msgA,msgB);
        }
    }
}
